package com.xdf.ecommerce.advice;

import com.xdf.ecommerce.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chanchaw
 * @create 2025-10-14 10:22
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
    /**
     * 注解 @ExceptionHandler 拦截全局异常后给自定义方法传入下面两个参数
     * 响应给用户异常信息同时记录日志
     * @param req 客户端发送来的请求
     * @param ex 异常
     * @return 统一响应对象
     */
    @ExceptionHandler(value = Exception.class)
    public CommonResponse<String> handlerCommerceException(HttpServletRequest req,Exception ex){
        CommonResponse<String> res = new CommonResponse(-1,"business exception");
        res.setData(ex.getMessage());
        log.error("catch global exception:[{}]", ex.getMessage(),ex);
        return res;
    }
}
